package perfumecabinet

import (
	"context"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/resultx"
	"mall/service/backend/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/rpc/forumclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreatePerfumeCabinetLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 添加香水柜
func NewCreatePerfumeCabinetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePerfumeCabinetLogic {
	return &CreatePerfumeCabinetLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreatePerfumeCabinetLogic) CreatePerfumeCabinet(req *types.PerfumeCabinetInfo) (resp *types.CreatePerfumeCabinetResp, err error) {
	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	if req.ObjectId == nil || *req.ObjectId == 0 {
		return nil, resultx.StatusError(resultx.FORUM_PERFUME_CABINET_OBJECT_ID_REQUIRED, "")
	}

	if req.ObjectType == nil || *req.ObjectType == 0 {
		return nil, resultx.StatusError(resultx.FORUM_PERFUME_CABINET_OBJECT_TYPE_REQUIRED, "")
	}

	res, err := l.svcCtx.ForumRpc.CreatePerfumeCabinet(l.ctx, &forumclient.PerfumeCabinetInfo{
		UserId:     pointy.GetPointer(userId),
		PlatformId: pointy.GetPointer(uint64(globalkey.ThirdPartyPlatformWechatMini)),
		ObjectId:   pointy.GetPointer(*req.ObjectId),
		ObjectType: pointy.GetPointer(uint32(*req.ObjectType)),
	})
	if err != nil {
		return nil, err
	}

	resp = &types.CreatePerfumeCabinetResp{
		Id:  res.Id,
		Msg: res.Msg,
	}

	return resp, nil
}
